package com.xh.one2one.base.service.impl;

import com.vividsolutions.jts.awt.PointShapeFactory;
import com.xh.bussiness.thirdapi.xinzhou.bean.request.XzStudentInfoRequest;
import com.xh.bussiness.thirdapi.xinzhou.model.XzContract;
import com.xh.bussiness.thirdapi.xinzhou.model.XzRemainContactHour;
import com.xh.bussiness.thirdapi.xinzhou.model.XzSchedule;
import com.xh.bussiness.thirdapi.xinzhou.model.XzStudentInfo;
import com.xh.bussiness.thirdapi.xinzhou.service.IXinzhouTmsService;
import com.xh.one2one.base.bean.dto.ContractDto;
import com.xh.one2one.base.bean.dto.StudentInfoDto;
import com.xh.one2one.base.bean.request.StudentInfoRequest;
import com.xh.one2one.base.model.StudentInfo;
import com.xh.one2one.base.dao.StudentInfoMapper;
import com.xh.one2one.base.service.IStudentInfoService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * <p>
 * 我的学员 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-05-17
 */
@Service
public class StudentInfoServiceImpl extends BaseServiceImpl<StudentInfoMapper, StudentInfo> implements IStudentInfoService {

    @Autowired
    private IXinzhouTmsService xinzhouTmsService;

    @Override
    public XhPage queryStudentInfoPage(XhPage page, StudentInfoRequest bean) {
        QueryWrapper qw=new QueryWrapper<StudentInfo>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryStudentInfo(StudentInfoRequest bean) {
        QueryWrapper qw=new QueryWrapper<StudentInfo>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public StudentInfoDto queryInfo(StudentInfoRequest bean) throws Exception {
        QueryWrapper qw=new QueryWrapper<StudentInfo>();
        //拼装条件
        qw=prepareParams(bean, qw);
        StudentInfo studentInfo = this.dao.selectOne(qw);
        XzStudentInfoRequest request = new XzStudentInfoRequest();
        request.setStudentId(bean.getStudentId());
        if(null == studentInfo ){
            studentInfo = new StudentInfo();
            List<XzStudentInfo> list = xinzhouTmsService.queryStudents(request);
            XzStudentInfo xzStudentInfo = list.get(0);
            studentInfo.setStudentId(xzStudentInfo.getStudentId());
            studentInfo.setStudentName(xzStudentInfo.getStudentName());
            studentInfo.setCampusName(xzStudentInfo.getSchoolName());
            studentInfo.setMobile(xzStudentInfo.getMobile());
        }
        List<XzRemainContactHour> contactHour = xinzhouTmsService.queryStudentRemainContactHour(request);
        String search = "一对一";
        if(2 == bean.getSearchType()){
            search = "一对六";
        }
        String finalSearch = search;
        int count = contactHour.stream().filter(xzRemainContactHour -> finalSearch.equals(xzRemainContactHour.getContractType()))
                .mapToInt(XzRemainContactHour::getRemainCount).sum();
        StudentInfoDto dto = new StudentInfoDto();
        BeanUtils.copyProperties(studentInfo,dto);
        dto.setClassHour(count);
        return dto;
    }

    @Override
    public List queryContract(StudentInfoRequest bean) throws Exception {

        XzStudentInfoRequest request = new XzStudentInfoRequest();
        request.setStudentId(bean.getStudentId());
        List<XzContract> list = xinzhouTmsService.queryStudentContract(request);

       List<ContractDto> retList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            XzContract contract = list.get(i);
            if("一对一".equals(contract.getContractType())){
                ContractDto dto = new ContractDto();
                dto.setContractId(contract.getContractOTOId());
                dto.setCreateTime(contract.getCreateTime());
                dto.setContractName(contract.getSubject());
                retList.add(dto);
            }
        }
        retList = retList.stream().collect(collectingAndThen(
                toCollection(() -> new TreeSet<>(Comparator.comparing(ContractDto::getContractId))), ArrayList::new));

        return retList;
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(StudentInfoRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(StringUtils.isNotEmpty(bean.getStudentId())){
            qw.eq("student_id",bean.getStudentId());
        }
        if(StringUtils.isNotEmpty(bean.getLessonId())){
            qw.eq("lesson_id",bean.getLessonId());
        }
        return qw;
    }
}
